package com.nari.ncps.screen.controller;

import com.nari.ncps.screen.common.pojo.ApiResponse;
import com.nari.ncps.screen.service.dryport.*;
import com.nari.ncps.screen.service.wholeproduct.QtDpIndustryProfileService;
import com.nari.ncps.screen.vo.dryport.*;
import com.nari.ncps.screen.vo.wholeproduct.FuelConsumptionVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 路港生产经营 前端控制器
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Tag(name = "路港生产经营", description = "路港生产经营")
@RestController
@RequestMapping("/dryport")
public class DryportController {

    @Resource
    private DryportProdoperGoodsSendService goodsSendService;

    @Resource
    private DryportProdoperOwnCoalService ownCoalService;

    @Resource
    private DryportProdoperFluxPlanService dryportProdoperFluxPlanService;

    @Resource
    private DryportProdoperTurnoverService dryportProdoperTurnoverService;

    @Resource
    private DryportProdoperPortGatherService dryportProdoperPortGatherService;

    @Resource
    private DryportProdoperThroughputService dryportProdoperThroughputService;

    @Resource
    private DryportProdoperTransshipmentService dryportProdoperTransshipmentService;

    @Resource
    private DryportOverviewItemService dryportOverviewItemService;

    @Resource
    private DryportOpermonLocomotiveService dryportOpermonLocomotiveService;

    @Resource
    private DryportOpermonCarService dryportOpermonCarService;

    @Resource
    private DryportOpermonShipService dryportOpermonShipService;

    @Resource
    private DryportSafetyenvLineService dryportSafetyenvLineService;

    @Resource
    private DryportSafetyenvSkylightService dryportSafetyenvSkylightService;

    @Resource
    private DryportSafetyenvLoadunloadService dryportSafetyenvLoadunloadService;

    @Resource
    private DryportDetailInfoService dryportDetailInfoService;

    @Resource
    private QtDpIndustryProfileService industryProfileService;

    /**
     * 生产经营-货物发送周转情况-自有煤炭情况
     * @return
     */
    @Operation(summary = "生产经营-货物发送周转情况", description = "生产经营-货物发送周转情况（最近30天日级别数据）")
    @GetMapping(value = "/operation/goods-turnover/statistics")
    public ApiResponse<List<GoodsSendVO>> goodsSendStatistics(
            @RequestParam(value = "year", required = false) Integer year
    ) {
        // 调用Service获取货物发送周转情况
        List<GoodsSendVO> data = goodsSendService.getGoodsSendStatistics(year);
        return ApiResponse.succeed(data);
    }

    /**
     * 生产经营-自有煤炭情况
     * @return
     */
    @Operation(summary = "生产经营-自有煤炭情况", description = "生产经营-自有煤炭情况（最近6个月，按月份从小到大排序）")
    @GetMapping(value = "/operation/goods-turnover/own-coal")
    public ApiResponse<List<OwnCoalVO>> ownCoalStatistics(
            @RequestParam(value = "year", required = false) Integer year) {
        // 调用Service获取自有煤炭情况
        List<OwnCoalVO> data = ownCoalService.getOwnCoalStatistics(year);
        return ApiResponse.succeed(data);
    }

    /**
     * 生产经营-引流计划统计
     * @param type 类型参数 0：引流计划 1：非煤计划
     * @return
     */
    @Operation(summary = "生产经营-引流/非煤计划统计, type：0 引流计划 1：非煤计划（最近12个月）", description = "生产经营-引流计划统计（最近12个月，按月份从小到大排序）")
    @GetMapping(value = "/operation/goods-turnover/drainage-plan")
    public ApiResponse<List<FluxPlanVO>> fluxPlanStatistics(
            @RequestParam(value = "year", required = false) Integer year,
            @RequestParam(value = "type", required = true) Integer type) {
        // 获取最近12个月的数据（按月份从小到大排序）
        List<FluxPlanVO> data = new ArrayList<>();
        if (type==0) {
            data = dryportProdoperFluxPlanService.getLatest12MonthsByType(type);
        } else {
            data = goodsSendService.getGoodsSendNocoalPlan(year);
        }
        return ApiResponse.succeed(data);
    }

    /**
     * 生产经营-周转计划情况
     * @param year 年份参数（已废弃，保留仅为兼容性）
     * @return
     */
    @Operation(summary = "生产经营-周转计划情况（最近6个月）", description = "生产经营-周转计划情况（最近6个月，按月份从小到大排序）")
    @GetMapping(value = "/operation/goods-turnover/turnover-plan")
    public ApiResponse<List<TurnoverVO>> turnoverStatistics(@RequestParam(value = "year", required = false) Integer year) {
        // 获取最近6个月的数据（按月份从小到大排序）
        List<TurnoverVO> data = dryportProdoperTurnoverService.getLatest6MonthsStats();
        return ApiResponse.succeed(data);
    }

    @Operation(summary = "生产经营-港口集疏港量完成情况-集港量（最近12个月）", description = "生产经营-港口集疏港量完成情况-集港量（最近12个月，按月份从小到大排序）")
    @GetMapping(value = "/operation/port-volume/collection")
    public ApiResponse<List<PortGatherVO>> portGatherStatistics(@RequestParam(value = "year", required = false) Integer year) {
        // 获取最近12个月的数据（按月份从小到大排序）
        List<PortGatherVO> data = dryportProdoperPortGatherService.getLatest12MonthsStats();
        return ApiResponse.succeed(data);
    }

    @Operation(summary = "生产经营-港口集疏港量完成情况-吞吐量（最近30天）", description = "生产经营-港口集疏港量完成情况-吞吐量（最近30天日级别数据，按日期从小到大排序）")
    @GetMapping(value = "/operation/port-volume/throughput")
    public ApiResponse<List<ThroughputVO>> throughputStatistics(@RequestParam(value = "year", required = false) Integer year) {
        // 获取最近30天的数据（按日期从小到大排序）
        List<ThroughputVO> data = dryportProdoperThroughputService.getLatest30DaysStats();
        return ApiResponse.succeed(data);
    }

    @Operation(summary = "生产经营-港口集疏港量完成情况-倒运量（最近30天）", description = "生产经营-港口集疏港量完成情况-倒运量（最近30天日级别数据，按日期从小到大排序）")
    @GetMapping(value = "/operation/port-volume/transshipment")
    public ApiResponse<List<TransshipmentVO>> transshipmentStatistics(@RequestParam(value = "year", required = false) Integer year) {
        // 获取最近30天的数据（按日期从小到大排序）
        List<TransshipmentVO> data = dryportProdoperTransshipmentService.getLatest30DaysStats();
        return ApiResponse.succeed(data);
    }

    @Operation(summary = "产业指标-产业顶部指标", description = "产业指标-产业顶部指标")
    @GetMapping(value = "/overview/main-indicators")
    public ApiResponse<OverviewVO> overviewStatistics(@RequestParam(value = "year", required = false) Integer year) {
        // 从数据库查询数据
        OverviewVO data = dryportOverviewItemService.getStatsByYear(year);
        return ApiResponse.succeed(data);
    }

    @Operation(summary = "运行监测-设备运行监测-机车整体运行情况", description = "运行监测-设备运行监测-机车整体运行情况")
    @GetMapping(value = "/monitoring/equipment/locomotive-status")
    public ApiResponse<LocomotiveStatusVO> locomotiveStatus(
            @RequestParam(value = "year", required = false) Integer year,
            @RequestParam(value = "month", required = false) Integer month,
            @RequestParam(value = "day", required = false) Integer day) {
        // 调用Service获取数据（默认查询昨天）
        LocomotiveStatusVO response = dryportOpermonLocomotiveService.getLocomotiveStatus(year, month, day);
        return ApiResponse.succeed(response);
    }


    @Operation(summary = "运行监测-设备运行监测-车辆运行情况监视", description = "运行监测-设备状态监测-车辆运行情况监视")
    @GetMapping(value = "/monitoring/equipment/vehicle-status")
    public ApiResponse<VehicleStatusVO> vehicleStatus(@RequestParam(value = "year", required = false) Integer year) {
        // 调用Service获取数据
        VehicleStatusVO response = dryportOpermonCarService.getVehicleStatus(year);
        return ApiResponse.succeed(response);
    }


    @Operation(summary = "运行监测-设备状态监测-来船信息", description = "运行监测-设备状态监测-来船信息")
    @GetMapping(value = "/shipInfoStatistics")
    public ApiResponse<List<ShipVO>> shipInfoStatistics(@RequestParam(value = "year", required = false) Integer year) {
        if (year == null) {
            year = 2025;
        }

        // 调用Service获取数据
        List<ShipVO> data = dryportOpermonShipService.getShipInfoStatistics(year);
        return ApiResponse.succeed(data);
    }


    @Operation(summary = "安全环保-施工检修-营业线路计划情况", description = "安全环保-施工检修-营业线路计划情况")
    @GetMapping(value = "/lineStatistics")
    public ApiResponse<List<LineVO>> lineStatistics(
            @RequestParam(value = "year", required = false) Integer year,
            @RequestParam(value = "month", required = false) Integer month,
            @RequestParam(value = "day", required = false) Integer day) {
        // 调用Service获取数据
        List<LineVO> data = dryportSafetyenvLineService.getLineStatistics(year, month, day);
        return ApiResponse.succeed(data);
    }

    // @Operation(summary = "安全环保-施工检修-今日计划兑现", description = "施工检修-节煤减排量-今日计划兑现")
    // @GetMapping(value = "/m14")
    // public ApiResponse<Object> m14(@RequestParam(value = "year", required = false) Integer year) {
    //     return ApiResponse.succeed();
    // }

    @Operation(summary = "安全环保-天窗修", description = "安全环保-天窗修")
    @GetMapping(value = "/skylightStatistics")
    public ApiResponse<SkylightVO> skylightStatistics(@RequestParam(value = "year", required = false) Integer year) {
        // 调用Service获取数据
        SkylightVO vo = dryportSafetyenvSkylightService.getSkylightStatistics(year);
        return ApiResponse.succeed(vo);
    }

    @Operation(summary = "安全环保-装车卸车完成情况", description = "安全环保-装车卸车完成情况")
    @GetMapping(value = "/loadUnloadStatistics")
    public ApiResponse<List<LoadUnloadVO>> loadUnloadStatistics(
            @RequestParam(value = "year", required = false) Integer year,
            @RequestParam(value = "month", required = false) Integer month,
            @RequestParam(value = "day", required = false) Integer day) {
        // 调用Service获取装车卸车数据
        List<LoadUnloadVO> voList = dryportSafetyenvLoadunloadService.getLoadUnloadStatistics(year, month, day);
//
//        voList.add(new LoadUnloadVO(){{
//            setDate("2025-10-31");
//            setLoadingStatus("10月31日7时至7时10分三号矿装车仓等大客通勤车过道口，影响26447次列车装车10分钟。");
//            setUnloadingStatus("10月31日21时50分至22时50分西港口CD1临时停电，影响26444次列车卸车1小时。");
//        }});
//
//        voList.add(new LoadUnloadVO(){{
//            setDate("2025-10-31");
//            setLoadingStatus("10月31日10时至10时50分二号矿装车仓等料，影响35155次列车装车50分钟。");
//            setUnloadingStatus("10月27日7时30分起西港口筒仓仓满，等船卸车，26316次列车7时28分至22时31分站内等卸");
//        }});

//        voList.add(new LoadUnloadVO(){{
//            setDate("2025-10-23");
//            setLoadingStatus("");
//            setUnloadingStatus("10月23日11时40分至12时30分新城热电仓满等卸，影响31748次列车卸车50分钟。");
//        }});
//
//        voList.add(new LoadUnloadVO(){{
//            setDate("2025-10-27");
//            setLoadingStatus("");
//            setUnloadingStatus("10月27日7时30分起西港口筒仓仓满，等船卸车，26316次列车7时28分至22时31分站内等卸。");
//        }});

//        return ApiResponse.succeed(data);
        return ApiResponse.succeed(voList);
    }

    @Operation(summary = "天气信息", description = "获取港口天气信息，包含日夜平均湿度、水温状况等")
    @GetMapping(value = "/weather/info")
    public ApiResponse<WeatherInfoVO> getWeatherInfo(@RequestParam(value = "orgCode", required = false) String orgCode) {
//        WeatherInfoVO vo = dryportDetailInfoService.getWeatherInfo(orgCode);
        WeatherInfoVO vo = new WeatherInfoVO();
        return ApiResponse.succeed(vo);
    }

    @Operation(summary = "列车实时信息", description = "获取列车实时信息，包含机车数、人员数、风速、速度等")
    @GetMapping(value = "/train/realtime")
    public ApiResponse<TrainRealtimeInfoVO> getTrainRealtimeInfo(@RequestParam(value = "orgCode", required = false) String orgCode) {

        //根据 各个列车的code 传递过来查询信息
        TrainRealtimeInfoVO vo = dryportDetailInfoService.getTrainRealtimeInfo(orgCode);
        return ApiResponse.succeed(vo);
    }

    @Operation(summary = "列车运行信息", description = "获取列车运行信息，包含车次、速度、里程等")
    @GetMapping(value = "/train/operation")
    public ApiResponse<TrainOperationInfoVO> getTrainOperationInfo(@RequestParam(value = "orgCode", required = false) String orgCode) {
        TrainOperationInfoVO vo = dryportDetailInfoService.getTrainOperationInfo(orgCode);
        return ApiResponse.succeed(vo);
    }

    @Operation(summary = "铁路运量信息", description = "获取铁路运量信息，包含日间、夜间、本月日均运量")
    @GetMapping(value = "/railway/transport")
    public ApiResponse<RailwayTransportInfoVO> getRailwayTransportInfo(@RequestParam(value = "orgCode", required = false) String orgCode) {
        RailwayTransportInfoVO vo = dryportDetailInfoService.getRailwayTransportInfo(orgCode);
        return ApiResponse.succeed(vo);

    }

    @Operation(summary = "铁路TQI", description = "获取铁路TQI指标，包含各月份TQI值")
    @GetMapping(value = "/railway/tqi")
    public ApiResponse<RailwayTqiVO> getRailwayTqi(@RequestParam(value = "orgCode", required = false) String orgCode) {
        RailwayTqiVO vo = dryportDetailInfoService.getRailwayTqi(orgCode);
        return ApiResponse.succeed(vo);
    }

    @Operation(summary = "港口信息", description = "获取港口动态信息")
    @GetMapping(value = "/port/info")
    public ApiResponse<PortInfoVO> getPortInfo(@RequestParam(value = "orgCode", required = false) String orgCode) {
        PortInfoVO vo = dryportDetailInfoService.getPortInfo(orgCode);
        return ApiResponse.succeed(vo);
    }

//    // 辅助方法：创建 TqiMonthItem 对象
//    private RailwayTqiVO.TqiMonthItem createTqiMonthItem(String month, String value) {
//        RailwayTqiVO.TqiMonthItem item = new RailwayTqiVO.TqiMonthItem();
//        item.setMonth(month);
//        item.setTqiValue(new BigDecimal(value));
//        return item;
//    }


}
